<template>
  <NuxtLayout name="admin" menuActive="/admin/curriculum">
    <div class="card">
      <el-page-header :content="infoData.title" @back="useRouter().go(-1)">
        <template #extra>
          <el-button @click="addFn">添加章节</el-button>
        </template>
      </el-page-header>
      <div style="height: 18px;"></div>
      <el-table class="my_table" :data="tableData" border show-overflow-tooltip>
        <el-table-column prop="name" label="章节名称" />
        <el-table-column prop="totalVideo" label="视频数量" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="sort" label="排序" />
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="scope">
            <el-button type="success" size="small" @click="editFn(scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="delFn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <MyForm :visible="formVisible" :forms="forms" :title="formTitle" @pick="formPickFn" />
  </NuxtLayout>
</template>

<script setup>
import { getCourseApi, addCourseChapter, updateCourseChapter, deleteCourseChapter } from '~/server/api'
import MyForm from './form'
// TDK
const prevHost = useRequestHeaders()["x-forwarded-proto"];
const host = useRequestHeaders()["host"];
const fullPath = useRoute()["fullPath"];
useSeoMeta({
  title: "技考邦_课程管理_章节管理",
  keywords: "技考邦,课程管理,章节管理",
  description: "技考邦，课程管理，章节管理。",
  ogTitle: "技考邦_课程管理_章节管理",
  ogDescription: "技考邦，课程管理，章节管理。",
  ogType: "article",
  ogUrl: `${prevHost}://${host}${fullPath}`,
});
// 获取详情
const tableData = ref([])
const infoData = ref({})
const getList = async () => {
  const { data: infoRes } = await useAsyncData('getCourseApi', () => useServerRequest(getCourseApi, {
    method: 'get',
    params: {
      courseId: useRoute().params.id
    }
  }))
  if (infoRes.value && infoRes.value.errno === 0 && infoRes.value.result) {
    tableData.value = infoRes.value.result.chapters;
    infoData.value = infoRes.value.result;
  }
}
getList()
// 添加
const formVisible = ref(false)
const formTitle = ref('')
const forms = ref({
  id: '',
  courseId: useRoute().params.id,
  name: '',
  sort: 0
})
const addFn = () => {
  forms.value.id = '';
  forms.value.name = '';
  forms.value.sort = 0;
  formTitle.value = '添加章节';
  formVisible.value = true;
}
// 编辑
const editFn = (val) => {
  forms.value.id = val.id;
  forms.value.name = val.name;
  forms.value.sort = val.sort;
  formTitle.value = '编辑章节';
  formVisible.value = true;
}
// 删除
const delFn = (val) => {
  ElMessageBox.confirm('是否删除当前数据', {
    showClose: false,
    closeOnClickModal: false,
    closeOnPressEscape: false
  }).then(async () => {
    let delRes = await useServerRequest(deleteCourseChapter, {
      method: 'post',
      body: JSON.stringify({
        id: val.id
      })
    })
    if (delRes.errno === 0) {
      ElMessage({
        message: '删除成功',
        type: 'success'
      })
      getList()
      return false;
    }
  }).catch(() => {})
}
// 取消 确认
const formPickFn = async (e) => {
  if (e.form) {
    const loading = ElLoading.service({ text: '请求中...', target: document.querySelector('.el-dialog') })
    let url = e.form.id ? updateCourseChapter : addCourseChapter;
    let teacherRes = await useServerRequest(url, {
      method: 'post',
      body: JSON.stringify(e.form)
    })
    if (teacherRes.errno === 0) {
      ElMessage({
        message: e.form.id ? '修改成功' : '添加成功',
        type: 'success'
      })
      getList()
      formVisible.value = e.visible;
    }
    loading.close()
    return false;
  }
  formVisible.value = e.visible;
}
</script>

<style lang="less" scoped>
.card {
  background: #FFFFFF;
  border-radius: 10px;
  border: 1px solid #EEEEEE;
  padding: 35px 25px;
}
</style>